﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCF20Model;
using WiseLink.RDS.Resource;

namespace CCF20Model.FaultHandler
{
    public class WillRepairCustomerFaultHandler2 : FaultHandlerBase
    {
        public WillRepairCustomerFaultHandler2(IFaultHandler faultHandler)
        {
            this.PreviousHandler = faultHandler;
            this.MyVehicle = faultHandler.MyVehicle;
            this.MyFault = faultHandler.MyFault;
            this.ccf20Ent = faultHandler.ccf20Ent;
        }
        public override void ProcessRequest()
        {
            IFaultHandler nextHandler = null;

            if (DateTime.Now - new TimeSpan(7, 0, 0, 0) < MyVehicle.LastContactTime)
            {
                if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent))
                {
                    LoggerHelper.LogZICDebug("[答应回场]-撞车请求-不管是什么状态下，都不屏蔽");
                    nextHandler = new ProcessMaintenaceHandler2(this);

                }
                else
                {
                    //忽略在过去三十天内，拒绝回场的用户的故障请求，里程（保养）,

                    VehicleRepairHistory vrh = MyVehicle.VehicleRepairHistories.OrderByDescending(r => r.OutShopDate).FirstOrDefault();
                    Fault previousFault = vrh.Faults.Where(f => f.Code!= null && f.Code.Contains(MyFault.Code)).FirstOrDefault();

                    if (previousFault != null)
                    {
                        CloseFault(MyVehicle.LastContactResult);
                        LoggerHelper.LogZICDebug(string.Format("[答应回场] {0}-客户上次答应回场={1}距离当前时间小于7天,并且上次答应回场时包含有相同的故障码 {2}-所以屏蔽",
                                                                      MyVehicle.LastContactResult,
                                                                      MyVehicle.LastContactTime,
                                                                      MyFault.Code));
                        CloseFault("上次答应回场时包含有相同的故障码");
                        
                    }
                    else
                    {
                        LoggerHelper.LogZICDebug(string.Format("[答应回场] {0}-客户上次答应回场={1}距离当前时间小于7天,但是上次答应回场时不包含有这个故障码 {2}-所以不屏蔽",
                                                                      MyVehicle.LastContactResult,
                                                                      MyVehicle.LastContactTime,
                                                                      MyFault.Code));
                        
                    }
                }
            }
            else
            {
                LoggerHelper.LogZICDebug(string.Format("[答应回场]-{0} 客户上次答应回场={1}距离当前时间大于7天-不屏蔽客户请求",
                                                             MyVehicle.LastContactResult,
                                                             MyVehicle.LastContactTime));
            }
            nextHandler = new ProcessMaintenaceHandler2(this);
            nextHandler.ProcessRequest();
        }
    }
}
